package com.lagou.web.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
import com.lagou.base.BaseServlet;
import com.lagou.pojo.Course;
import com.lagou.pojo.Course_Lesson;
import com.lagou.pojo.Course_Section;
import com.lagou.service.CourseContentService;
import com.lagou.service.impl.CourseContentServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author TianSu
 * @creat 2021-05-11-16:06
 **/

@WebServlet("/courseContent")
public class CourseContentServlet extends BaseServlet {

    public void findSectionAndLessonByCourseId(HttpServletRequest request, HttpServletResponse response) {
        try {
            //1获取参数
            String course_id = request.getParameter("course_id");

            //2业务处理
            CourseContentServiceImpl contentService = new CourseContentServiceImpl();
            List<Course_Section> list = contentService.findSectionAndLessonByCourseId(Integer.parseInt(course_id));
            //3返回结果
            String result = JSON.toJSONString(list);
            response.getWriter().print(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //根据课程id回显课程信息
    public void findCourseById(HttpServletRequest request,HttpServletResponse response){
        //1获取参数
        try {
            String course_id = request.getParameter("course_id");

            //2业务处理
            CourseContentServiceImpl courseContentService = new CourseContentServiceImpl();
            Course course = courseContentService.findCourseByCourseId(Integer.parseInt(course_id));

            //3返回json数据
            SimplePropertyPreFilter filter = new SimplePropertyPreFilter(Course.class, "id", "course_name");
            String result = JSON.toJSONString(course, filter);
            response.getWriter().print(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    //保存修改章节信息
    public void saveorUpdateSection(HttpServletRequest request,HttpServletResponse response){

        try {
            //1获取参数,从域对象中获取
            Map<String,Object> map = (Map)request.getAttribute("map");

            //2创建course_section
            Course_Section section = new Course_Section();

            //3使用BeanUtils工具类，将map中的数据封装到section
            BeanUtils.populate(section,map);

            //4业务处理
            CourseContentService contentService = new CourseContentServiceImpl();

            //判断是否又Id
            if(section.getId() ==0){
                //新增操作
                String result = contentService.saveSection(section);
                response.getWriter().print(result);
            }else {
                //修改操作
                String result = contentService.updateSection(section);
                response.getWriter().print(result);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //修改章节状态
    public void updateSectionStatus(HttpServletRequest request,HttpServletResponse response){
        try {
            //1获取参数
            int id = Integer.parseInt(request.getParameter("id"));
            int status = Integer.parseInt(request.getParameter("status"));

            //4业务处理
            CourseContentService contentService = new CourseContentServiceImpl();
            String result = contentService.updateSectionStatus(id, status);

            response.getWriter().print(result);

            } catch (Exception e) {
                e.printStackTrace();
            }
    }

    //保存修改课时信息
    public void saveOrUpdateLesson(HttpServletRequest request,HttpServletResponse response){
        try {
            //1获取参数,从域对象中获取
            Map<String,Object> map = (Map)request.getAttribute("map");

            //2创建Course_Lesson
            Course_Lesson lesson = new Course_Lesson();

            //3使用BeanUtils工具类，将map中的数据封装到lesson
            BeanUtils.populate(lesson,map);

            //4业务处理
            CourseContentService contentService = new CourseContentServiceImpl();

            //判断是否又Id
            if(lesson.getId() ==0){
                //新增操作
                String result = contentService.saveLesson(lesson);
                response.getWriter().print(result);
            }else {
                //修改操作
                String result = contentService.updateLesson(lesson);
                response.getWriter().print(result);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
